﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class GUI_ViewRequest : System.Web.UI.Page
{
    string username;
    int userid;
    //int rowNum;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["uid"] == null)
        {
            Response.Redirect("../GUI/Home.aspx");
        }
        else
        {
            username = Session["uid"].ToString();
            userid = ToolsDT.getUserID(username);
            DataTable dt = ToolsDT.ShowInvitationPenDing(userid);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
    protected void RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strCommandName = e.CommandName.ToString();
        int intIndex = int.Parse(e.CommandArgument.ToString());
        int id = int.Parse(GridView1.DataKeys[intIndex].Value.ToString());
        int userRequestID = int.Parse((GridView1.Rows[intIndex].Cells[2].Text.ToString()));
        if (strCommandName == "accept")
        {
            ToolsDT.acceptInvite(id, userid, userRequestID);
            Response.Write(@"<script language='javascript'>alert('Add friend successful.')</script>");       
        }
    }
    protected void Deleting(object sender, GridViewDeleteEventArgs e)
    {

        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        SqlConnection cn = ToolsDT.getConnect();
        SqlCommand cmd = new SqlCommand("sp_DeleteInvite", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.ExecuteNonQuery();
        cn.Close();
        cmd.Dispose();
        DataTable dt = ToolsDT.ShowInvitationPenDing(userid);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
